﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-04-02 22:29
    /// 描 述：产品入库记录业务类
    /// </summary>
    public class GoodsStorageRecordBLL
    {
        private GoodsStorageRecordService goodsStorageRecordService = new GoodsStorageRecordService();
        private GoodsWarehouseService goodsWarehouseService = new GoodsWarehouseService();
        #region 获取数据
        public async Task<TData<List<GoodsStorageRecordEntity>>> GetList(GoodsStorageRecordListParam param)
        {
            TData<List<GoodsStorageRecordEntity>> obj = new TData<List<GoodsStorageRecordEntity>>();
            obj.Data = await goodsStorageRecordService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<GoodsStorageRecordEntity>>> GetPageList(GoodsStorageRecordListParam param, Pagination pagination)
        {
            TData<List<GoodsStorageRecordEntity>> obj = new TData<List<GoodsStorageRecordEntity>>();
            obj.Data = await goodsStorageRecordService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<GoodsStorageRecordEntity>> GetEntity(long id)
        {
            TData<GoodsStorageRecordEntity> obj = new TData<GoodsStorageRecordEntity>();
            obj.Data = await goodsStorageRecordService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(GoodsStorageRecordEntity entity)
        {
            TData<string> obj = new TData<string>();
            if (entity.productId.IsNullOrZero())
            {
                obj.Tag = 0;
                obj.Message = "货物信息异常";
                return obj;
            }
            if (entity.goodsId.IsNullOrZero())
            {
                obj.Tag = 0;
                obj.Message = "商品信息异常";
                return obj;
            }

            //更新货物
            var pdata=goodsWarehouseService.GetEntity(entity.productId.Value).Result;

            if (pdata==null)
            {
                obj.Tag = 0;
                obj.Message = "货物信息异常";
                return obj;
            }

            pdata.current_cnt = pdata.current_cnt + entity.storageCount;
            pdata.in_cnt= pdata.in_cnt + entity.storageCount;
            await goodsWarehouseService.SaveForm(pdata);
            await goodsStorageRecordService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await goodsStorageRecordService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
